#include "stdafx.h"
#include "PlayerSkill.h"
PlayerSkill::PlayerSkill(PlayerId stPlayerId)
	:PlayerItem(stPlayerId)
{
	LOGGER_INFO("PlayerSkill:%p,",this);
}

PlayerSkill::~PlayerSkill()
{
	LOGGER_INFO("~PlayerSkill:%p,",this);
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		singleton<SkillManager>::instance().Remove(m_arSkills[i]);
	}
}

void PlayerSkill::addSkill(Int32 csv_id)
{
	if (m_arSkills.size() >= MAX_SKILLS_SIZE)
	{
		LOGGER_ERROR("addSkill,m_arSkills.size() >= CharInfo::MAX_SKILLS_SIZE");
		return;
	}

	Skill *pSkill = singleton<SkillManager>::instance().CreateFromCSVId(csv_id);
	if (pSkill == NULL)
	{
		LOGGER_ERROR("addSkill,pSkill == NULL,csv_id:%d",csv_id);
		return;
	}
	m_arSkills.push_back(pSkill->GetSkillId());
	Int32 type = pSkill->GetSkillCSVInfo()->type;
	if(m_stCharInfo.m_arDefaultSkills[DEFAULT_SKILL_CONTROL] == 0)
		if(type == Skill::SKILL_TYPE_HL || type == Skill::SKILL_TYPE_BD || type == Skill::SKILL_TYPE_HS)
			m_stCharInfo.m_arDefaultSkills[DEFAULT_SKILL_CONTROL] = csv_id;
	if(m_stCharInfo.m_arDefaultSkills[DEFAULT_SKILL_ATTACK] == 0)
		if(type == Skill::SKILL_TYPE_SL || type == Skill::SKILL_TYPE_JZ || type == Skill::SKILL_TYPE_SD)
			m_stCharInfo.m_arDefaultSkills[DEFAULT_SKILL_ATTACK] = csv_id;
	if(m_stCharInfo.m_arDefaultSkills[DEFAULT_SKILL_ASSIST] == 0)
		if(type == Skill::SKILL_TYPE_QS || type == Skill::SKILL_TYPE_CF || type == Skill::SKILL_TYPE_JS)
			m_stCharInfo.m_arDefaultSkills[DEFAULT_SKILL_ASSIST] = csv_id;
}
bool PlayerSkill::hasSkill(Int32 csv_id)
{
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill != NULL)
		{
			if(pSkill->GetSkillInfo().csv_id == csv_id)
				return true;
		}
	}
	return false;
}
Skill* PlayerSkill::getSkill(Int32 csv_id)
{
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill != NULL)
		{
			if(pSkill->GetSkillInfo().csv_id == csv_id)
			{
				return pSkill;
			}
		}
	}
	return NULL;
}
void PlayerSkill::decodeSkills()
{
	if (m_stCharInfo.m_nSkillsSize > MAX_SKILLS_SIZE)
	{
		LOGGER_ERROR("decodeSkills,m_stCharInfo.m_nSkillsSize > CharInfo::MAX_SKILLS_SIZE");
		return;
	}
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		singleton<SkillManager>::instance().Remove(m_arSkills[i]);
	}
	m_arSkills.clear();
	Skill *pSkill = NULL;
	for (UInt32 i = 0; i < m_stCharInfo.m_nSkillsSize; i++)
	{
		if (m_stCharInfo.m_arSkills[i].csv_id != 0 )
		{
			pSkill = singleton<SkillManager>::instance().CreateFromSkillInfo(m_stCharInfo.m_arSkills[i]);
			if (pSkill == NULL)
			{
				LOGGER_ERROR("decodeSkills,m_stCharInfo.m_arSkills[i].mastery != 0,but pSkill == NULL,csv_id:%d",m_stCharInfo.m_arSkills[i].csv_id);
			}
			else
			{
				m_arSkills.push_back(pSkill->GetSkillId());
				pSkill->Update((Player*)this);
			}
		}
	}
}
void PlayerSkill::encodeSkills()
{
	if (m_arSkills.size() > MAX_SKILLS_SIZE)
	{
		LOGGER_ERROR("encodeSkills,m_arSkills.size() > CharInfo::MAX_SKILLS_SIZE");
		return;
	}

	m_stCharInfo.m_nSkillsSize = m_arSkills.size();
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill != NULL)
		{
			m_stCharInfo.m_arSkills[i] = pSkill->GetSkillInfo();
		}
	}
}
void PlayerSkill::sendSkills()
{
	PacketEncoder encoder;
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(21));

	encoder.AddItem(Int32(m_arSkills.size()));
	for (UInt32 i = 0; i < m_arSkills.size(); i++)
	{
		Skill *pSkill = singleton<SkillManager>::instance().Get(m_arSkills[i]);
		if (pSkill == NULL)
			continue;
		SkillInfo& info = pSkill->GetSkillInfo();
		encoder.AddItem(info.csv_id);
		encoder.AddItem(info.level);
		encoder.AddItem(pSkill->m_nLevelUpMoney);
		encoder.AddItem(pSkill->m_nTargetCount);
		encoder.AddItem(pSkill->m_nRoundCount);
		encoder.AddItem(pSkill->m_nSuccessRate);
		encoder.AddItem(pSkill->m_nConsumeHPRate);
	}
	for (Int32 i = 0; i < DEFAULT_SKILL_SIZE; i++)
	{
		encoder.AddItem(m_stCharInfo.m_arDefaultSkills[i]);
	}
	SendPacket(m_stPlayerId.nSocketId,m_stPlayerId.nSocketIdOnGate,encoder);

}
